package com.fly.controller;

import com.fly.bean.Task;
import com.fly.bean.Users;
import com.fly.service.TaskServiceO;
import com.fly.service.UserService;
import com.fly.util.JWTUtil;
import com.fly.util.Msg;
import com.fly.util.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Auther: Orchid
 * @Date: 2019/11/27 19:27
 * @Description:
 */
@Controller
@RequestMapping("/taskOrchid")
public class TaskControllerO {

    @Resource
    TaskServiceO taskServiceO;

    @Resource
    UserService userService;

    /**
     * 分页查询任务信息
     * @param page 实体类
     * @return 返回分页数据，总页面数
     */
    @RequestMapping("/getTaskByUserId")
    @ResponseBody
    public Msg getTaskByUsersId(Page page, HttpServletRequest request){

        String token = request.getHeader("Authorization");
//
        Users users = new Users();
        //获取登录用户的对象信息
        users = JWTUtil.unsign(token, Users.class);

        if (users == null){
            return Msg.fail().add("error", "访问错误");
        }

//        users.setUserId(10000046);


        //判断用户是否是超级管理员
        Boolean isSuper = taskServiceO.queryUserRoleIsSuper(users.getUserId());

        if (isSuper){
            users.setUserId(null);
        }

        //根据用户id查询分页查询任务
        List<Task> tasks = taskServiceO.queryTaskByUsersId(users.getUserId(), page);

        //根据用户id查询任务总数
        Integer count = taskServiceO.queryCountTaskByUserId(users);

        page.setCount(count);

        //返回信息
        Msg msg = Msg.success().add("count", count);
        msg.add("tasks",tasks);

        return msg;
    }

    /**
     * 查看详细任务信息
     * @param taskId
     * @return
     */
    @RequestMapping("/getTaskByTaskId")
    @ResponseBody
    public Msg getTaskByUsersId(@RequestParam("taskId") Integer taskId){

        Task task = new Task();

        task = taskServiceO.queryTaskByTaskId(taskId);

        return Msg.success().add("task", task);
    }

}
